﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.Runtime.CompilerServices;
using System.Threading;
using NetFrameworkExtensions.Enterprise.Properties;

namespace NetFrameworkExtensions.Enterprise.Security
{
  [Serializable]
  public class DbIdentity<TIdentity, TUserId> :
    IdentityBase<TIdentity>,
    IDbIdentity<TUserId>
    where TIdentity : DbIdentity<TIdentity, TUserId>
  {
    public DbIdentity(TUserId userId, string name, string authenticationType)
      : base(name, authenticationType)
    {
      UserId = userId;
    }

    public DbIdentity()
    {
      UserId = default(TUserId);
    }

    public TUserId UserId { get; private set; }

    object IDbIdentity.UserId { get { return UserId; } }

    protected override bool EqualsOverride(TIdentity other)
    {
      return base.EqualsOverride(other) && EqualityComparer<TUserId>.Default.Equals(UserId, other.UserId);
    }

    protected override int GetHashCodeOverride()
    {
      unchecked
      {
        return base.GetHashCodeOverride() * 37 ^ UserId.GetHashCode();
      }
    }
  }
}
